﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using framework.util;
namespace framework.ui
{
    public partial class PackageManager : Form
    {

        string[] maps = new string[25], shots = new string[25], mtrls = new string[25];
        int mcounter = 0, scounter = 0, mtrlcounter = 0;

        public PackageManager()
        {
            InitializeComponent();
            this.AllowDrop = true;
            this.DragEnter += new DragEventHandler(PackageManager_DragEnter);
            this.DragDrop += new DragEventHandler(PackageManager_DragDrop);          
        }

        private void PackageManager_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
        }

        private void PackageManager_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (tabControl1.SelectedTab.Text == "New Package")
            {
                foreach (string f in files)
                {
                    string nm = Path.GetFileName(f);
                    switch (Path.GetExtension(f))
                    {
                        case ".map":
                            treeView1.Nodes.Find("Nodo0", false)[0].Nodes.Add(new TreeNode(nm));
                            maps[mcounter] = f;
                            mcounter++;
                            break;
                        case "":
                            treeView1.Nodes.Find("Nodo1", false)[0].Nodes.Add(new TreeNode(nm));
                            mtrls[mtrlcounter] = f;
                            mtrlcounter++;
                            break;
                        case ".png":
                            treeView1.Nodes.Find("Nodo2", false)[0].Nodes.Add(new TreeNode(nm));
                            shots[scounter] = f;
                            break;
                        default:
                            break;
                    }
                }
            }
            else if (tabControl1.SelectedTab.Text == "Package Inspector")
            {
                PackageInpector inspector = new PackageInpector(files[0]);
                inspector.Size = new Size(344, 332);
                this.tabPage2.Controls.Add(inspector);

                // Close the dragger and delete the picturebox with the "plus"
                this.AllowDrop = false;
                this.pictureBox1.Dispose();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            if (of.ShowDialog() == DialogResult.OK)
            {
                string nm = Path.GetFileName(of.FileName);
                switch (Path.GetExtension(of.FileName))
                {
                    case ".map":
                        treeView1.Nodes.Find("Nodo0", false)[0].Nodes.Add(new TreeNode(nm));
                        maps[mcounter] = of.FileName;
                        mcounter++;
                        break;
                    case "":
                        treeView1.Nodes.Find("Nodo1", false)[0].Nodes.Add(new TreeNode(nm));
                        mtrls[mtrlcounter] = of.FileName;
                        mcounter++;
                        break;
                    case ".png":
                        shots[scounter] = of.FileName;
                        mcounter++;
                        treeView1.Nodes.Find("Nodo2", false)[0].Nodes.Add(new TreeNode(nm));
                        break;
                    default:
                        break;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Zip File (*.zip)|*.zip|All Files (*.*)|*.*";
            if(save.ShowDialog() == DialogResult.OK)
                File.Copy(@"temp/pack.zip", save.FileName);
        }

        private void treeView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 8)
                treeView1.SelectedNode.Remove();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!(textBox2.Text == "" || textBox1.Text == "" || richTextBox1.Text == ""))
            {
                try
                {
                    ZipHandler.CreateZip(textBox2.Text, CleanArray(maps),
                                                        CleanArray(mtrls),
                                                        CleanArray(shots),
                                         @"temp\pack.zip");
                    this.toolStripStatusLabel1.Text = "Package ready for exporting...";
                }
                catch (Exception ex)
                {
                    this.toolStripStatusLabel1.Text = ex.Message;
                }
            }
            else this.toolStripStatusLabel1.Text = "Some information is missing!";
        }

        private string[] CleanArray(string[] array)
        {
            string[] ret = new string[array.Length];
            int counter = 0;
            for(int i = 0; i < array.Length; i++) 
            {
                if (array[i] != null)
                {
                    ret[counter] = array[i];
                    counter++;
                }
            }
            counter = 0;
            foreach (string elem in ret)
            {
                if (elem == null)
                {
                    break;
                }
                counter++;
            }
            string[] fret = new string[counter];
            for(int i = 0; i < counter; i++) {
                fret[i] = ret[i];
            }
            return fret;
        }

    }
}